package com.ruoyi.student.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.student.domain.Address;
import com.ruoyi.student.domain.StudentAll;
import com.ruoyi.student.mapper.AddressMapper;
import com.ruoyi.system.domain.StudentInformation;
import com.ruoyi.system.mapper.StudentInformationMapper;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.student.mapper.StudentMapper;
import com.ruoyi.student.domain.Student;
import com.ruoyi.student.service.IStudentService;
import org.springframework.transaction.annotation.Transactional;

/**
 * studentOptService业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-19
 */
@Service
public class StudentServiceImpl implements IStudentService 
{
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private StudentInformationMapper studentInformationMapper;
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private SysRoleMapper roleMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private ISysUserService userService;

    /**
     * 查询studentOpt
     * 
     * @param studentId studentOpt主键
     * @return studentOpt
     */
    @Override
    public Student selectStudentByStudentId(Long studentId)
    {
        return studentMapper.selectStudentByStudentId(studentId);
    }

    @Override
    public Student selectStudentByUserName(String username) {
        return studentMapper.selectStudentByUserName(username);
    }

    @Override
    public Student selectStudentByUserId(Long id) {
        return studentMapper.selectStudentByUserId(id);
    }

    /**
     * 查询studentOpt列表
     * 
     * @param student studentOpt
     * @return studentOpt
     */
    @Override
    public List<Student> selectStudentList(Student student,Long userId)
    {
        List<SysRole> sysRoles = roleMapper.selectRolePermissionByUserId(userId);
        System.out.println(sysRoles);
        for (SysRole sysRole : sysRoles) {
            if(sysRole.getRoleId() == 3) {
                SysUser sysUser = sysUserMapper.selectUserById(userId);
                return studentMapper.selectStudentByDeptId(sysUser.getDeptId());
            }
        }
        return studentMapper.selectStudentList(student);
    }

    @Override
    @Transactional
    public int addStudentAll(StudentAll studentAll) {
        Student student = BeanUtil.copyProperties(studentAll, Student.class);
        StudentInformation studentInformation = BeanUtil.copyProperties(studentAll, StudentInformation.class);
        int i1 = studentMapper.updateStudent(student);
        int i = 0;
        System.out.println(student);
        System.out.println(studentInformation);
        if(studentInformation == null) {
            return 1;
        }
        if(studentInformationMapper.selectStudentInformationByUid(student.getUid()) != null) {
            i = studentInformationMapper.updateStudentInformation(studentInformation);
        }else {
            i = studentInformationMapper.insertStudentInformation(studentInformation);
        }
        return i > 0 && i1 > 0 ? 1 : 0;
    }

    /**
     * 新增studentOpt
     * 
     * @param student studentOpt
     * @return 结果
     */
    @Override
    @Transactional
    public int insertStudent(Student student)
    {
        SysUser sysUser = new SysUser();
        sysUser.setNickName(student.getStudentName());
        sysUser.setPassword(SecurityUtils.encryptPassword("123456"));
        sysUser.setUserName(student.getStudentName());
        int i = sysUserMapper.insertUser(sysUser);
        student.setUid(sysUser.getUserId());
        Long[] longs = new Long[1];
        longs[0] = 4L;
        userService.insertUserAuth(sysUser.getUserId(),longs);
        return studentMapper.insertStudent(student);
    }
    @Override
    public List<Address> getAddress() {
        return build(addressMapper.getAddress());
    }
    private List<Address> build(List<Address> addresses){
        List<Address> returnList = new ArrayList<>();
        List<Long> tempList = addresses.stream().map(Address::getId).toList();
        for (Address menu : addresses) {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(menu.getParentId())) {
                recursionFn(addresses, menu);
                returnList.add(menu);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = addresses;
        }
        return returnList;
    }
    private void recursionFn(List<Address> list, Address t)
    {
        // 得到子节点列表
        List<Address> childList = getChildList(list, t);
        t.setChildren(childList);
        for (Address tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }
    private boolean hasChild(List<Address> list, Address t)
    {
        return getChildList(list, t).size() > 0;
    }
    private List<Address> getChildList(List<Address> list, Address t)
    {
        List<Address> tlist = new ArrayList<>();
        for (Address n : list) {
            if (n.getParentId().longValue() == t.getId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }
    /**
     * 修改studentOpt
     * 
     * @param student studentOpt
     * @return 结果
     */
    @Override
    public int updateStudent(Student student)
    {
        return studentMapper.updateStudent(student);
    }

    /**
     * 批量删除studentOpt
     * 
     * @param studentIds 需要删除的studentOpt主键
     * @return 结果
     */
    @Override
    public int deleteStudentByStudentIds(Long[] studentIds)
    {
        return studentMapper.deleteStudentByStudentIds(studentIds);
    }

    /**
     * 删除studentOpt信息
     * 
     * @param studentId studentOpt主键
     * @return 结果
     */
    @Override
    public int deleteStudentByStudentId(Long studentId)
    {
        return studentMapper.deleteStudentByStudentId(studentId);
    }
}
